home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-01-28 | 22.9 KB | 998 lines | [TEXT/MPS ] |
- /*
- File: SimpliFace.cp
-
- Contains: A simple scriptable application class.
-
- Developed by:
-
- Paul G Smith (commstalk hq & Full Moon Software, Inc)
-
- you can leave messages at (UK): 0727 844232; (US): 408 253 7199
- BUT I prefer to be contacted by e-mail
- AppleLink: SMITH.PG
- Internet: SMITH.PG@applelink.apple.com
-
- "SimpliFace" Sample code to accompany develop article
- on techniques for embedding scripts in applications.
-
- Important note on Segmentation strategy:
- There is none. This program is compiled with -model far
- (MPW 32-bit everything). If you convert it to Think C++
- you'll need to segment it.
-
- */
-
-
- #ifndef __SimpliFace__
- #include <SimpliFace.h>
- #endif
-
- #ifndef __SimpliFaceCOMMON__
- #include "SimpliFaceCommon.h"
- #endif
-
- #ifndef __APPLICATIONCOMMON__
- #include "ApplicationCommon.h"
- #endif
-
- #ifndef __STDIO__
- #include <StdIO.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
- #ifndef __WINDOWS__
- #include <Windows.h>
- #endif
- #ifndef __DIALOGS__
- #include <Dialogs.h>
- #endif
- #ifndef __QUICKDRAW__
- #include <Quickdraw.h>
- #endif
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
- #ifndef __PACKAGES__
- #include <Packages.h>
- #endif
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
- #ifndef __FILES__
- #include <Files.h>
- #endif
- #ifndef __STANDARDFILE__
- #include <StandardFile.h>
- #endif
- #ifndef __GESTALTEQU__
- #include <GestaltEqu.h>
- #endif
- #ifndef __SYSEQU__
- #include <SysEqu.h>
- #endif
- #ifndef __PLSTRINGFUNCS__
- #include <PLStringFuncs.h>
- #endif
-
- #ifndef __AEOBJECTS__
- #include <AEObjects.h>
- #endif
-
- #ifndef __AEOBJECTPACKING__
- #include <AEPackObject.h>
- #endif
-
- #ifndef __AERegistry__
- #include <AERegistry.h>
- #endif
- #ifndef __ASREGISTRY__
- #include <ASRegistry.h>
- #endif
-
- #ifndef __AEOMTOKENS__
- #include "ObjModelTokens.h"
- #endif
- #ifndef __AEOMEVENTS__
- #include "ObjModelEvents.h"
- #endif
-
- #ifndef __SCRIPTUTILS__
- #include "ScriptUtils.h"
- #endif
-
- #ifndef __WINDOWOBJ__
- #include "WindowObj.h"
- #endif
-
- extern "C" {
- #include "AEBuild.h"
- #include "AEBuildGlobals.h"
- #include "AEPrint.h"
- }
- #include "AEBuildErrMsgs.h"
-
-
- #include "DebugTrace.h"
-
-
- #define kMinStackSize 40000
- #define kMinHeapSize 150000
-
- #define kWNEsleepTicks 10
-
- #define kSaveGlobVars false
-
-
- /* some globals we need for the static methods */
-
- TSimpliFace* gSimpliFace = NULL;
- Boolean gDone = false;
- Boolean gQuit = false;
-
- int main()
- {
- gSimpliFace = new TSimpliFace(Ptr(&qd));
-
- AEObjectInit();
-
- // Start our main event loop running.
- while (!gQuit && gSimpliFace)
- {
- gSimpliFace->EventLoop();
- gQuit = true;
- }
- if (gSimpliFace)
- {
- delete gSimpliFace;
- gSimpliFace = NULL;
- }
-
- // We always return a value, like good little ANSI worshippers
- return 0;
- }
-
- /**********************************************************************
- ** SimpliFace static methods
- ***********************************************************************/
-
- #define optionKeyCode 58
- static Boolean isKeyDown(long keyCode);
- static void AlertUser(short errResID, short errCode);
-
- /**********************************************************************
- ** isKeyDown
- ***********************************************************************/
-
- static Boolean isKeyDown(long keyCode)
- {
- KeyMap keys;
- Byte *keysBytes;
-
- GetKeys(keys);
-
- keysBytes = (Byte *)keys;
-
- return((keysBytes[keyCode/8] & (1 << keyCode % 8)) != 0);
- }
-
- /**********************************************************************
- ** AlertUser
- ***********************************************************************/
-
- void AlertUser(short errResID, short errCode)
- {
- Str255 message;
-
- GetIndString(message, errResID, errCode);
- #if qDebug
- if (message[0] == 0)
- {
- DebugStr((ConstStr255Param)"\pProgram error: could not get error string.");
- return;
- }
- #endif
- ParamText(message, (ConstStr255Param)"\p", (ConstStr255Param)"\p", (ConstStr255Param)"\p");
- Alert(rUserAlert, NULL);
- }
-
-
- /**********************************************************************
- ** CheckGestaltOSA: return false if we can't load
- ***********************************************************************/
-
- Boolean CheckGestaltOSA(void)
- {
- OSErr err = 0;
- long ascrAttr = 0;
- long ascrVers = 0;
- char ascrVersStr[24];
-
- err = Gestalt('ascr', &ascrAttr);
- if (!err)
- {
- err = Gestalt('ascv', &ascrVers);
- sprintf(ascrVersStr, "%.8lx", ascrVers);
- printf("AppleScript version %.2s.%.1s.%.1s [$%s]\n",
- &ascrVersStr[0], &ascrVersStr[2], &ascrVersStr[3], ascrVersStr);
- }
- else
- printf("AppleScript not present\n");
-
- return (err == 0);
- }
-
- /**********************************************************************
- ** PUBLIC Constructor
- ***********************************************************************/
-
- TSimpliFace::TSimpliFace(Ptr qdPtr) : TApplication(qdPtr)
- {
- InstallWriteLnHook(); // debug assistance, so printf() works for us
- ForceDebug();
-
- if (CheckGestaltOSA())
- {
- // read menus into menu bar
-
- Handle menuBar = GetNewMBar(rMenuBar);
- // install menus
- SetMenuBar(menuBar);
-
- // add DA names to Apple menu
- AddResMenu(GetMHandle(mApple),'DRVR');
- DrawMenuBar();
-
- // create empty mouse region
- fMouseRgn = NewRgn();
- // make sure we have a valid cursor region
- AdjustCursor();
- }
- else
- TApplication::BigBadError(kApplicationErrStrings,eNeedAS); // if not, alert & quit
- }
-
- TSimpliFace::~TSimpliFace()
- {
- StopScriptAdministrator(); // do it here in case we have a forced exit
- DisposHandle(Handle(fMouseRgn));
- }
-
- /**********************************************************************
- ** PUBLIC SetUp/Cleanup
- ***********************************************************************/
-
- void TSimpliFace::SetUp()
- { // Run before event loop starts
- OSAError err = 0;
-
- err = StartScriptAdministrator();
-
- }
-
- void TSimpliFace::CleanUp()
- {
- StopScriptAdministrator();
- }
-
- /**********************************************************************
- ** PUBLIC SleepVal
- ***********************************************************************/
-
- unsigned long TSimpliFace::SleepVal()
- {
- return kWNEsleepTicks; // how long to sleep in WaitNextEvent
- }
-
-
- /**********************************************************************
- ** PUBLIC DoIdle
- ***********************************************************************/
-
- void TSimpliFace::DoIdle()
- {
- }
-
-
- Boolean TSimpliFace::HandleGoAway (AEDesc *target, WindowPtr tWind)
- {
- Boolean wasHandled = false;
- OSErr err = 0;
- AppleEvent event, reply;
- ProcessSerialNumber aSelfPSN = gSelfPSN;
- CStr255 aeOtherParams = "'savo':'ask '";
-
- err = AEBuildAppleEvent(kAECoreSuite, kAEClose,
- typeProcessSerialNumber, &aSelfPSN,
- sizeof(aSelfPSN), kAutoGenerateReturnID,
- kAnyTransactionID, &event,
- (char*)aeOtherParams);
- if (!err)
- err = AEPutParamDesc(&event, keyDirectObject, target);
-
- if (!err)
- {
- err = AESend(&event, &reply, kAENoReply+kAEAlwaysInteract,
- kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
- AEDisposeDesc(&event);
- wasHandled = (err == noErr);
- }
-
- return wasHandled;
- }
-
-
- Boolean TSimpliFace::HandleContentClick (AEDesc *target,
- WindowPtr tWind, EventRecord& theEvent)
- {
- Boolean wasHandled = false;
- Point clickPt = theEvent.where;
- ControlHandle whichControl = NULL;
-
- GlobalToLocal(&clickPt);
-
- short controlPart = FindControl(clickPt, tWind, &whichControl);
-
- if (!controlPart ||
- (controlPart && whichControl && TrackControl(whichControl, clickPt, NULL)))
- {
- OSErr err = 0;
- AppleEvent event, reply;
- ProcessSerialNumber aSelfPSN = gSelfPSN;
- CStr255 theHPos, theVPos;
- NumToString(theEvent.where.h, theHPos);
- NumToString(theEvent.where.v, theVPos);
- CStr255 aeOtherParams = "'PEVK':'EMOU',"
- "'data':[“" + theHPos + "”,"
- "“" + theVPos + "”]";
- err = AEBuildAppleEvent(kSignature, kAESystemEvent,
- typeProcessSerialNumber, &aSelfPSN,
- sizeof(aSelfPSN), kAutoGenerateReturnID,
- kAnyTransactionID, &event,
- (char*)aeOtherParams);
- if (!err)
- err = AEPutParamDesc(&event, keyDirectObject, target);
-
- if (!err)
- {
- err = AESend(&event, &reply, kAENoReply+kAEAlwaysInteract,
- kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
- AEDisposeDesc(&event);
- wasHandled = (err == noErr);
- }
- }
- return wasHandled;
- }
-
-
- Boolean TSimpliFace::HandleMenuCommand(AEDesc *target,
- short menuNum, short itemNum)
- {
- Boolean wasHandled = false;
- MenuHandle thisMenu = GetMHandle(menuNum);
- CStr255 menuName, itemName;
-
- if (thisMenu && *thisMenu)
- {
- GetItem(thisMenu, itemNum, itemName);
- menuName = ((*thisMenu)->menuData);
- }
- else
- {
- menuName = "";
- itemName = "";
- }
-
- if (menuName != "" && itemName != "")
- {
- OSErr err = 0;
- AppleEvent event, reply;
- ProcessSerialNumber aSelfPSN = gSelfPSN;
- CStr255 aeOtherParams = "'PEVK':'EMEN',"
- "'data':[“" + menuName + "”,"
- "“" + itemName + "”]";
- err = AEBuildAppleEvent(kSignature, kAESystemEvent,
- typeProcessSerialNumber, &aSelfPSN,
- sizeof(aSelfPSN), kAutoGenerateReturnID,
- kAnyTransactionID, &event,
- (char*)aeOtherParams);
- if (!err)
- err = AEPutParamDesc(&event, keyDirectObject, target);
-
- if (!err)
- {
- err = AESend(&event, &reply, kAENoReply+kAEAlwaysInteract,
- kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
- AEDisposeDesc(&event);
- wasHandled = (err == noErr);
- }
- }
- return wasHandled;
- }
-
-
- Boolean TSimpliFace::HandleMouseDown(AEDesc *target, EventRecord& theEvent)
- {
- Boolean wasHandled = false;
- long mResult;
- short partCode;
- WindowPtr tWind;
-
- // gotta watch those object field dereferences
- partCode = FindWindow(theEvent.where, &tWind);
- switch (partCode)
- {
- case inMenuBar :
- AdjustMenus();
- mResult = MenuSelect(theEvent.where);
- if (mResult != 0)
- {
- wasHandled = HandleMenuCommand(target, HiWord(mResult), LoWord(mResult));
- if (!wasHandled) // hack! we are anticipating EventLoop():
- { // this is to avoid calling MenuSelect() twice
- DoMenuCommand(HiWord(mResult),LoWord(mResult));
- wasHandled = true;
- }
- }
- break;
- case inGoAway :
- wasHandled = HandleGoAway(target, tWind);
- break;
- case inContent :
- // If window is not in front, make it so
- if ( tWind != FrontWindow() )
- SelectWindow(fWhichWindow);
- else
- wasHandled = HandleContentClick(target, tWind, theEvent);
- break;
- }
-
- return wasHandled;
- }
-
-
- Boolean TSimpliFace::HandleKeyDown(AEDesc *target, EventRecord& theEvent)
- {
- Boolean wasHandled = false;
- char key = (char) (fTheEvent.message & charCodeMask);
-
- if ((fTheEvent.modifiers & cmdKey) && (fTheEvent.what == keyDown))
- {
- // only do command keys if we are not autokeying
- AdjustMenus(); // make sure menus are up to date
- long mResult = MenuKey(key);
- if (mResult != 0) // if it wasn't a menu key, pass it through
- wasHandled = HandleMenuCommand(target, HiWord(mResult), LoWord(mResult));
- }
- else
- {
- OSErr err = 0;
- AppleEvent event, reply;
- ProcessSerialNumber aSelfPSN = gSelfPSN;
- CStr255 theKeyPressed;
- theKeyPressed[1] = key;
- theKeyPressed[0] = 1;
- CStr255 aeOtherParams = "'PEVK':'EKEY',"
- "'data':“" + theKeyPressed + "”";
- err = AEBuildAppleEvent(kSignature, kAESystemEvent,
- typeProcessSerialNumber, &aSelfPSN,
- sizeof(aSelfPSN), kAutoGenerateReturnID,
- kAnyTransactionID, &event,
- (char*)aeOtherParams);
- if (!err)
- err = AEPutParamDesc(&event, keyDirectObject, target);
-
- if (!err)
- {
- err = AESend(&event, &reply, kAENoReply+kAEAlwaysInteract,
- kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
- AEDisposeDesc(&event);
- wasHandled = (err == noErr);
- }
- }
-
- return wasHandled;
- }
-
-
- Boolean TSimpliFace::HandleEvent(EventRecord& theEvent, Boolean& /*pass*/)
- {
- Boolean wasHandled = false;
- AEDesc target;
-
- InitAEDescs(&target, kEndOfList);
-
- switch (theEvent.what)
- {
- case mouseDown :
- if (GetTargetObjectSpecifier(theEvent, &target) == noErr)
- wasHandled = HandleMouseDown(&target, theEvent);
- break;
- case keyDown :
- case autoKey :
- if (GetTargetObjectSpecifier(theEvent, &target) == noErr)
- wasHandled = HandleKeyDown(&target, theEvent);
- break;
- } // end switch (fTheEvent.what)
-
- DisposeAEDescs(&target, kEndOfList);
-
- return wasHandled;
- }
-
-
- OSErr TSimpliFace::CollectAETEs(short /*languageCode*/, AEDesc& resultDesc)
- {
- OSErr err = noErr;
- Handle aeutH = NULL;
-
- aeutH = GetResource(typeAETE, 0);
- if (aeutH)
- {
- char hState = HGetState(aeutH);
- HLock(aeutH);
- err = AEPutPtr(&resultDesc, 0, typeAETE, *aeutH, GetHandleSize(aeutH));
- HSetState(aeutH, hState);
- }
- else
- err = -192; /* resNotFound */
-
- return err;
- }
-
-
- /**********************************************************************
- ** PUBLIC AdjustMenus
- ***********************************************************************/
-
- // Enable and disable menus based on the current state. The
- // user can only select enabled menu items. We set up all the
- // menu items before calling MenuSelect or MenuKey, since
- // these are the only times that a menu item can be selected.
- // Note that MenuSelect is also the only time the user will
- // see menu items. This approach to deciding what enable/
- // disable state a menu item has the advantage of
- // concentrating all the decision-making in one routine, as
- // opposed to being spread throughout the application. Other
- // application designs may take a different approach that may
- // or may not be as valid.
-
- void ExactAppend(MenuHandle aMenu, Str255 aString)
- {
- unsigned char fillString[128];
- /* Since AppendMenu will interpret meta-characters, first append a menu item with */
- /* no meta-characters, but which has the same length as aString, then set the menu */
- /* item to astring (SetItem doesn't interpret meta-characters). */
- fillString[0] = aString[0];
- if (aString[0])
- memset(fillString + 1, 'a', aString[0]);
- AppendMenu(aMenu, fillString);
- SetItem(aMenu, CountMItems(aMenu), aString);
- }
-
- void TSimpliFace::AdjustMenus(void)
- {
- WindowPtr frontmost;
- MenuHandle menu;
- Boolean undo;
- Boolean cutCopyClear;
- Boolean paste;
-
- frontmost = FrontWindow();
-
- /* Edit menu */
-
- menu = GetMHandle(mEdit);
- undo = false;
- cutCopyClear = false;
- paste = false;
- if (frontmost != NULL) {
- undo = true; // all editing is enabled for DA windows
- cutCopyClear = true;
- paste = true;
- }
-
- if (undo)
- EnableItem(menu, iUndo);
- else
- DisableItem(menu, iUndo);
-
- if (cutCopyClear) {
- EnableItem(menu, iCut);
- EnableItem(menu, iCopy);
- EnableItem(menu, iClear);
- }
- else {
- DisableItem(menu, iCut);
- DisableItem(menu, iCopy);
- DisableItem(menu, iClear);
- }
- if (paste)
- EnableItem(menu, iPaste);
- else
- DisableItem(menu, iPaste);
-
- /* File Menu */
-
- menu = GetMHandle(mFile);
- if (fWindowObjects.CountElements() > 0)
- EnableItem(menu, iClose);
- else
- DisableItem(menu, iClose);
- EnableItem(menu,iQuit);
- }
-
- /**********************************************************************
- ** PUBLIC AdjustCursor
- ***********************************************************************/
-
- void TSimpliFace::AdjustCursor()
- {
- Boolean handled = false;
-
- if (!handled)
- SetCursor(&fqd->arrow);
- }
-
- /**********************************************************************
- ** PUBLIC DoMenuCommand
- ***********************************************************************/
-
- // This is called when an item is chosen from the menu bar (after calling
- // MenuSelect or MenuKey). It does the right thing for each command.
-
- void TSimpliFace::DoMenuCommand(short menuID, short menuItem)
- {
- short itemHit;
- Str255 daName;
- short daRefNum;
- WindowPtr window;
-
- window = FrontWindow();
- switch (menuID) {
- case mApple:
- switch (menuItem) {
- case iAbout: // bring up alert for About
- itemHit = Alert(rAboutAlert, NULL);
- break;
- default: // all non-About items in this menu are DAs et al
- GetItem(GetMHandle(mApple), menuItem, daName);
- daRefNum = OpenDeskAcc(daName);
- break;
- }
- break;
-
- case mFile:
- switch (menuItem) {
- case iQuit:
- Terminate();
- break;
- case iClose:
- TWindowObj *theWindObj = (TWindowObj*)(((WindowPeek)fWhichWindow)->refCon);
- if (theWindObj)
- theWindObj->CloseObject();
- else
- CloseDeskAcc(((WindowPeek) fWhichWindow)->windowKind);
- break;
- }
- break;
-
- case mEdit: // call SystemEdit for DA editing & MultiFinder
- if (!SystemEdit(menuItem-1))
- DebugStr((ConstStr255Param)"\pOops. SystemEdit returned false.");
- break;
-
- }
- HiliteMenu(0); // unhighlight what MenuSelect (or MenuKey) hilited
- }
-
-
-
- void TSimpliFace::GetThisAppName(CStr255& /*appName*/)
- {
- //GetCurrAppName(appName);
- }
-
-
- /**********************************************************************
- ** PUBLIC Terminate
- ***********************************************************************/
-
- // Clean up the application and exits. You might want to close all
- // of your documents (and ask the user to save them) here.
-
- void TSimpliFace::Terminate(void)
- {
- ExitLoop();
- } // Terminate
-
-
- /**********************************************************************
- ** PUBLIC StackNeeded/HeapNeeded
- ***********************************************************************/
-
- long TSimpliFace::StackNeeded()
- {
- return kMinStackSize;
- }
-
- long TSimpliFace::HeapNeeded()
- {
- return kMinHeapSize;
- }
-
-
- /**********************************************************************
- ** PUBLIC DoGoAway
- ***********************************************************************/
-
- void TSimpliFace::DoGoAway(void)
- {
- TWindowObj *theWindObj = (TWindowObj*)(((WindowPeek)fWhichWindow)->refCon);
- if (theWindObj)
- {
- if (TrackGoAway(fWhichWindow, fTheEvent.where))
- theWindObj->CloseObject();
- }
- else
- TApplication::DoGoAway();
- }
-
- /**********************************************************************
- ** PUBLIC DoActivateEvt/DoUpdateEvt
- ***********************************************************************/
-
- void TSimpliFace::DoActivateEvt(void)
- {
- // event record contains window ptr
- fWhichWindow = (WindowPtr) fTheEvent.message;
- SetPort(fWhichWindow);
-
- TWindowObj *theWindObj = (TWindowObj*)(((WindowPeek)fWhichWindow)->refCon);
- if (theWindObj)
- theWindObj->ActivateWindow((fTheEvent.modifiers & activeFlag) != 0);
- }
-
- void TSimpliFace::DoUpdateEvt(void)
- {
- // event record contains window ptr
- fWhichWindow = (WindowPtr) fTheEvent.message;
- SetPort(fWhichWindow);
-
- TWindowObj *theWindObj = (TWindowObj*)(((WindowPeek)fWhichWindow)->refCon);
- if (theWindObj)
- theWindObj->UpdateWindow();
- }
-
- /**********************************************************************
- ** PUBLIC AE Object Model support
- ***********************************************************************/
-
-
- OSErr TSimpliFace::CountElements(DescType desiredClass, long *result)
- {
- OSErr err = errAEEventNotHandled;
-
- if (desiredClass == cWindow)
- {
- *result = fWindowObjects.CountElements();
- err = 0;
- }
- return err;
- }
-
-
- OSErr TSimpliFace::GetProperty (DescType propertyID, DescType wantType, AEDesc *result)
- {
- OSErr err = errAEEventNotHandled;
- CStr255 theName;
-
- switch (propertyID)
- {
- case pName:
- GetCurrAppName(theName);
- err = AECreateDesc(typeChar, (Ptr)&theName[1], theName.Length(), result);
- break;
- case pVersion:
- theName = kVersion;
- err = AECreateDesc(typeChar, (Ptr)&theName[1], theName.Length(), result);
- break;
- case pIsFrontProcess:
- Boolean theBoolean = false;
- ProcessSerialNumber PSN1, PSN2;
- err = GetFrontProcess(&PSN1);
- if (!err)
- err = GetCurrentProcess(&PSN2);
- if (!err)
- err = SameProcess(&PSN1, &PSN2, &theBoolean);
- if (!err)
- err = AECreateDesc(typeBoolean, (Ptr)&theBoolean,
- sizeof(theBoolean), result);
- break;
- default:
- err = TScriptableObject::GetProperty(propertyID, wantType, result);
- break;
- }
-
- return err;
- }
-
-
- OSErr TSimpliFace::SetProperty (DescType propertyID, const AEDesc *theData)
- {
- OSErr err = errAEEventNotHandled;
-
- switch (propertyID)
- {
- default:
- err = TScriptableObject::SetProperty(propertyID, theData);
- break;
- }
-
- return err;
- }
-
-
- OSErr TSimpliFace::CreateNewElement (DescType desiredClass,
- DescType position,
- AEDesc *theData,
- AERecord *theProperties,
- TScriptableObject *theContainerObj,
- TScriptableObject **theNewObj)
- {
- OSErr err = errAEEventNotHandled;
-
- if (desiredClass == cWindow)
- {
- TWindowObj *newWindowObj = NULL;
-
- if (theProperties)
- newWindowObj = new TWindowObj(theProperties);
- else
- {
- newWindowObj = new TWindowObj();
- if (newWindowObj && theData)
- newWindowObj->SetProperty(pName, theData);
- }
- if (newWindowObj)
- {
- fWindowObjects.InsertElement((long)newWindowObj);
- *theNewObj = newWindowObj;
- err = 0;
- }
- }
-
- return err;
- }
-
-
- OSErr TSimpliFace::ResolveContainer(TScriptableObject **theContainerObj)
- {
- OSErr err = 0;
-
- *theContainerObj = NULL;
-
- return err;
- }
-
-
- OSErr TSimpliFace::ResolveElementByName(DescType desiredClass,
- CStr255& nameStr,
- TScriptableObject **theResultObj)
- {
- OSErr err = errAEEventNotHandled;
- WindowPtr theWindow = NULL;
-
- theWindow = WindowNameToWindowPtr((StringPtr)nameStr);
- if (theWindow)
- {
- // we are making a possibly dangerous assumption: that all windows are ours!
- *theResultObj = (TScriptableObject*)(((WindowPeek)theWindow)->refCon);
- if (*theResultObj != NULL)
- err = 0;
- }
- else
- {
- TWindowObj *aWindowObj = NULL;
- long numWindows = fWindowObjects.CountElements();
- CStr255 aName;
-
- while (numWindows > 0)
- {
- aWindowObj = (TWindowObj*)(fWindowObjects.GetElement(numWindows));
- if (aWindowObj)
- {
- aWindowObj->GetName(aName);
- if (aName == nameStr)
- {
- *theResultObj = aWindowObj;
- numWindows = 0;
- err = 0;
- }
- }
- numWindows--;
- }
- }
-
- return err;
- }
-
-
- OSErr TSimpliFace::ResolveElementByIndex(DescType desiredClass,
- short theIndex,
- TScriptableObject **theResultObj)
- {
- OSErr err = errAEEventNotHandled;
- WindowPtr theWindow = NULL;
- CStr255 nameStr = "";
- short index = theIndex;
-
- if (index<0)
- index = CountWindows()+index+1;
-
- theWindow = GetWindowPtrOfNthWindow(index);
- if (theWindow)
- {
- // we are making a possibly dangerous assumption: that all windows are ours!
- *theResultObj = (TScriptableObject*)(((WindowPeek)theWindow)->refCon);
- if (*theResultObj != NULL)
- err = 0;
- }
-
- return err;
- }
-
-
-
-
- OSErr TSimpliFace::GetTargetObjectSpecifier (EventRecord& theEvent, AEDesc *result)
- {
- WindowPtr theWindow = FrontWindow();
-
- if (theEvent.what == mouseDown)
- FindWindow(theEvent.where, &theWindow);
-
- if (theWindow)
- {
- SetPort(theWindow);
- // we are making a possibly dangerous assumption: that all windows are ours!
- TWindowObj *aWindowObj = (TWindowObj*)(((WindowPeek)theWindow)->refCon);
- if (aWindowObj)
- return aWindowObj->GetTargetObjectSpecifier(theEvent, result);
- }
- else
- {
- OSErr err = 0;
- AEDesc containerDesc, newContainerDesc;
-
- err = MakeNullDesc(&containerDesc);
-
- if (!err)
- {
- CStr255 progName;
- AEDesc nameDesc;
-
- GetCurrAppName(progName);
- err = MakeNameDesc(progName, &nameDesc);
- if (!err)
- {
- err = CreateObjSpecifier(cApplication, &containerDesc, formName,
- &nameDesc, true, &newContainerDesc);
- containerDesc = newContainerDesc;
- }
- }
- *result = containerDesc;
- return err;
- }
- }
-